import Vue from 'vue'
import Router from 'vue-router'
import NProgress from 'nprogress'

import 'nprogress/nprogress.css'

Vue.use(Router)
var router = new Router({
  routes: [
    // login后面不设置index.vue也可以
    // index.vue index.js设置为默认索引文件,他们会自动寻找到
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login')
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/home'),
      redirect: '/welcome',
      children: [
        {
          path: '/fans', name: 'fans', component: () => import('@/views/fans')
        },
        {
          path: '/material', name: 'material', component: () => import('@/views/material')
        },
        {
          path: '/welcome', name: 'welcome', component: () => import('@/views/welcome')
        },
        {
          path: '/article', name: 'article', component: () => import('@/views/article')
        },
        {
          path: '/articleadd', name: 'articleadd', component: () => import('@/views/articleadd')
        },
        {
          path: '/articleedit/:aid', name: 'articleedit', component: () => import('@/views/articleedit')
        },
        {
          path: '/account', name: 'account', component: () => import('@/views/account')
        }
      ]

    }
  ]
})

router.beforeEach((to, from, next) => {
  NProgress.set(0.4)
  var token = window.sessionStorage.getItem('token')
  if (!token && to.path !== '/login') {
    return next('/login')
  }
  next()
})
router.afterEach((to, from) => {
  NProgress.done()
})

export default router
